package com.dapeis.api.controller.collocation.impl;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.core.exception.SysTipsException;
import com.ag.module.common.api.domain.NoticePush;
import com.ag.module.common.api.service.NoticePushService;
import com.ag.utils.MessageFormatter;
import com.ag.utils.NumberUtils;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.api.controller.RestController;
import com.dapeis.api.controller.collocation.ISettingsController;
import com.dapeis.core.utils.StringUtil;
import com.dapeis.domain.Feedback;
import com.dapeis.domain.ext.CollocationSupplyExt;
import com.dapeis.service.BankService;
import com.dapeis.service.CollocationFeedbackService;
import com.dapeis.service.CollocationService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/dps/settings")
public class SettingsControllerImpl extends RestController implements
	ISettingsController {

    @Resource
    CollocationService collocationService;

    @Resource
    CollocationFeedbackService feedbackService;

    @Resource
    BankService supplymsgService;

    @Resource
    NoticePushService noticePushService;

    @Override
    public JsonElement saveFeedback(String feedback, String contact,
	    HttpServletRequest request) {
	if (StringUtil.isNotBlank(feedback)) {
	    Feedback feed = new Feedback(feedback,
		    getThreadLocalCollocationId());
	    if (StringUtil.isNotBlank(contact)) {
		feed.setContact(contact);
	    }
	    Boolean result = feedbackService.insertSelective(feed) > 0;
	    if (result) {
		JsonObject json = buildSuccess();
		return json;
	    }
	    throw new SysTipsException("提交意见反馈服务器异常,请重试!");
	}
	throw new InvalidParameterException("反馈意见内容不能为空!");
    }


    @Override
    public JsonElement getMessage(
	    @RequestParam(required = false) Integer curretPager,
	    HttpServletRequest request) {
	Pager pager = new Pager();
	if(NumberUtils.isValid(curretPager)){
	    pager.setCurrentPage(curretPager);
	}
	Example example = new Example(NoticePush.class);
	Criteria criteria = example.createCriteria();
	List deviceTypes = new ArrayList();
	deviceTypes.add("all");
	deviceTypes.add("ios");
	criteria.andIn(NoticePush.PROPERTY_DEVICETYPE, deviceTypes);
	/**
	 * select * from notice_push where deviceType in ('all','ios') or
	 * terminalIds like '%20%' limit 1,15
	 */
	String sql = " deviceType in ('all','ios') OR terminalIds like '%"
		+ getThreadLocalCollocationId() + "%'";

	criteria.andCustomCondition(sql, null);
	
	pager.setExample(example);

	noticePushService.pager(pager);
	JsonObject json = buildSuccess();
	json.add("pager", AGsonBuilder.create().toJsonTree(pager));
	return json;
    }

}
